/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "../connection.h"
#include "../serverconnection.h"

using namespace greenhills::io;

CServerConnection::CServerConnection()
{
}

CServerConnection::~CServerConnection()
{
    close();
}

void CServerConnection::sendMessage(const char *pData, unsigned int pLength)
{
//    mMutex.lock();

    std::vector<CConnection*>::iterator it;

    for (it = mConnectionList.begin();
         it != mConnectionList.end(); it++) {

        try {
            (*it)->sendMessage(pData, pLength);
        } catch ( ... ) {
            mConnectionList.erase(it);
            it--;
        }
    }

//    mMutex.unlock();
}

void CServerConnection::process()
{
    std::vector<CConnection*>::iterator it;

    mMutex.lock();

    for (it = mConnectionList.begin();
         it != mConnectionList.end(); it++) {

        try {
            (*it)->process();
        } catch ( ... ) {
            mConnectionList.erase(it);
            it--;
        }
    }

    mMutex.unlock();
}

void CServerConnection::close()
{
    std::vector<CConnection*>::iterator it;

    mMutex.lock();

    for (it = mConnectionList.begin();
         it != mConnectionList.end(); it++) {

        if (*it != NULL) {
            (*it)->close();
            delete *it;
        }
    }

    mConnectionList.clear();

    mMutex.unlock();
}

void CServerConnection::dataReceived(const char *pData,
                                     unsigned int pLength,
                                     CConnection *pConnection)
{
    emitDataReceivedEvent(pData, pLength, pConnection);
}

void CServerConnection::addDataListener(IDataListener *pListener)
{
    if (pListener == NULL) {
        return;
    }

    std::vector<IDataListener*>::iterator it;

    mMutex.lock();

    for (it = mDataListenerList.begin(); it != mDataListenerList.end(); it++) {
        if (*it == pListener) {
            mMutex.unlock();
            return;
        }
    }

    mDataListenerList.push_back(pListener);
    mMutex.unlock();
}

void CServerConnection::removeDataListener(IDataListener *pListener)
{
    if (pListener == NULL) {
        return;
    }

    std::vector<IDataListener*>::iterator it;

    mMutex.lock();

    for (it = mDataListenerList.begin(); it != mDataListenerList.end(); it++) {
        if (*it == pListener) {
            mDataListenerList.erase(it);
            break;
        }
    }

    mMutex.unlock();
}

void CServerConnection::removeAllDataListeners()
{
    mMutex.lock();
    mDataListenerList.clear();
    mMutex.unlock();
}

void CServerConnection::emitDataReceivedEvent(const char *pData,
                                              unsigned int pLength,
                                              CConnection *pConnection)
{
    std::vector<IDataListener*>::iterator it;

    for (it = mDataListenerList.begin(); it != mDataListenerList.end(); it++) {
        (*it)->dataReceived(pData, pLength, pConnection);
    }
}
